package com.engr.lyx.auth.client.exception;


import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

/**
 * 自定义oauth2异常提示
 * @author lyx
 */
public class Oauth2WebResponseExceptionTranslator implements WebResponseExceptionTranslator {

    private WebResponseExceptionTranslator defaultTranslator = new DefaultWebResponseExceptionTranslator();

    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {

        ResponseEntity<OAuth2Exception> responseEntity = this.defaultTranslator.translate(e);
        OAuth2Exception body = new Oauth2Exception(e.getMessage());
        body.addAdditionalInformation("code", String.valueOf(responseEntity.getStatusCode()));
        HttpHeaders headers = new HttpHeaders();
        headers.setAll(responseEntity.getHeaders().toSingleValueMap());
        // do something with header or response
        return new ResponseEntity<>(body, headers, responseEntity.getStatusCode());
    }
}